<?php
/**
 * 文章操作类
 * @version $Id$
 */
class ArticleAction extends BaseAction{
	protected $Article;
	protected $Product;
	function _initialize(){
		parent::_initialize();
		$this->Article = M("ArticleB2b");
		import("Com.Product");
		$this->Product = new Product();
		
	}
	
	/**
	 * 载入文章查询类
	 * @date 2013-11-5
	 * @time 下午1:48:31
	 */
	function index(){
		$where = array();
		$catid = $_REQUEST['catid'];
		$cte = $this->Product->catetochildren($catid);
		$cte = $this->Product->arr2to1($cte,'id');
		$where['catid'] = array('in',$cte);
		$where = self::create_where($where);
		
		$count=$this->Article->where($where)->count();
	
		$this->assign("totalCount",$count);
		if($count>0){
			$page=new Page($count,$this->listrows);
			$list=$this->Article->join(' as a left join product_category_tag as pct on a.catid = pct.id')
			->field('a.* ,pct.name')
			->where($where)->order($this->order)->limit($page->firstRow.','.$page->listRows)->select();
			
			$this->assign('list',$list);
		}
		$this->assign('catid',$catid);
		$this->display();
	}
	
	/**
	 * 添加文章
	 * @date 2013-11-5
	 * @time 下午1:48:31
	 */
	function add(){
		$catid = $_REQUEST['catid'];
		$cateinfo = $this->Product->catetochildren($catid);
		$id = $_REQUEST['id'];
		if($id){
			$article = $this->Article->find($id);
		}
		$this->assign('cateinfo',$cateinfo);
		$this->assign('article',$article);
		$this->display();
	}
	
	/**
	 * 添加文章数据
	 * @date 2013-11-6
	 * @time 下午1:48:31
	 */
	function insert(){
		$data = $_REQUEST['data'];
		$data['userid'] = $this->userid;
		$data['username'] = $this->username;
		$data['updatetime'] = time();
		if($data['id']){
			$reid = $this->Article->save($data);
		}else{
			$data['createtime'] = time();
			$reid = $this->Article->add($data);
		}
		
		if(($data[id] && $reid>=0) || (!$data['id'] && $reid>0)){
			$this->_ajm(200,'保存成功！',1);
		}else{
			$this->_ajm(300,'保存失败！',1);
		}
		
	}
	
	/**
	 * 删除文章数据
	 * @date 2013-11-6
	 * @time 下午1:48:31
	 */
	function del(){
		$id = $_GET['id'];
		if(!$id){
			$this->_ajm(300,'选择商品错误！',1);
		}
		$reid = $this->Article->delete($id);
		if($reid>0){
			$this->_ajm(200,'删除成功！',1);
		}else {
			$this->_ajm(300,'删除失败！',1);
		}
	}
	

}
